var t = getApp()
Page({
  data: {
    userInfo:wx.getStorageSync('userInfo'),
    iconsUrl:t.globalData.iconsUrl,
    info:null,
    days:0,
    about_us:'',
    center_picture:'',
    user_agreement:'',
    order_list:[{
      icon:'/icons/person_send_out.png',
      name:'待发货',
    },{
      icon:'/icons/person_take_over.png',
      name:'待收货',
    },{
      icon:'/icons/person_done.png',
      name:'已完成',
    },{
      icon:'/icons/person_all.png',
      name:'全部',
    },{
      icon:'/icons/person_about_us.png',
      name:'联系我们',
    },{
      icon:'/icons/person_set.png',
      name:'设置',
    }],
    navHeight:t.globalData.navHeight,
    title:'我的',
    // 获取手机号弹窗参数
    show: false,
    duration: 300, //动画时长，单位毫秒
    position: 'bottom',
    round: true,  //是否显示圆角
    overlay: true, //是否显示遮罩层
    customStyle: '',//自定义弹出层样式
    overlayStyle: '' //自定义遮罩层样式
  },
  onLoad: function (options) {
    // this.login = this.selectComponent('#login')
    // if (t.globalData.show_login) {t.show_login();return;}
    this.get_info();
  },
  // 显示电话登录的弹窗
  showTelLogin(){
    let that = this
    that.setData({show: true})
    // 隐藏底部 tabbar
    wx.hideTabBar({
      animation: false, // 是否需要动画效果，默认为 false
      success: function () {
        console.log('TabBar has been hidden successfully!');
        that.setData({show: true})
      },
      fail: function (err) {
        console.error('Failed to hide TabBar:', err);
      }
    });
  },
  // 登录成功，关闭方法
  exit(){
    this.setData({show: false})
    // 显示底部tabbar
    wx.showTabBar({
      animation: false, // 是否需要动画效果，默认为 false
      success: function () {console.log('TabBar has been hidden successfully!');},
      fail: function (err) {console.error('Failed to hide TabBar:', err);}
    });
  },
  // 手机号 暂不登录按钮
  exitback() {
    this.setData({show: false})
    // 显示底部tabbar
    wx.showTabBar({
      animation: false, // 是否需要动画效果，默认为 false
      success: function () {
        console.log('TabBar has been hidden successfully!');
        wx.switchTab({
          url: '/pages/index/index',
        })       
      },
      fail: function (err) {
        console.error('Failed to hide TabBar:', err);
      }
    });
  },
  //电话登录
  getPhoneNumber(e){
    const that = this,
      iv = e.detail.iv,
      da = e.detail.encryptedData;
    if (!e.detail.encryptedData) return wx.showToast({ title: '授权失败,请重新授权！',icon:'none'})
    // 请求接口
    wx.login({
      success(e){
        console.log(e);
        wx.showLoading({
          title: '请稍后',
        })
        t.request('/login/getPhoneNumber',{code: e.code,iv:iv,encryptedData:da},function(event){
          wx.hideLoading({})
          if (event.code!==200) return wx.showToast({ title: event.msg,icon:'none' })
          if (event.code==200) {
            wx.setStorageSync('tel', event.result.phoneNumber)
            t.globalData.show_tel_login =false
            that.exit(false) //不返回首页
          }
        })
      }
    })
  },
  onShow(e){
    this.setData({
      userInfo:wx.getStorageSync('userInfo'),
    })
    
    this.get_day();
    this.getsite();
    //判断手机是否登录了
    if (t.globalData.show_tel_login) {
      this.showTelLogin()
      return;
    }
  },
  onRefresh(){
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading(); 
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    this.get_info();
    this.get_day();
  },
  onPullDownRefresh: function () {
    //调用刷新时将执行的方法
    this.onRefresh();
    setTimeout(function () {
      // 不加这个方法真机下拉会一直处于刷新状态，无法复位
      wx.stopPullDownRefresh()
    }, 1000)
  },
  getsite(){
    let that=this;
    t.request('/Users/getEntry',{},function(r){
          console.log(r.result);
          that.setData({
            about_us:r.result.about_us,
            center_picture:r.result.center_picture,
            user_agreement:r.result.user_agreement
          })
    })
  },
  getUserProfile(e) {
    let that=this;
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
    // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res.userInfo);
        
        t.request('/users/up',{nick_name:res.userInfo.nickName,avatar:res.userInfo.avatarUrl},function(r){
          wx.setStorageSync('userInfo',res.userInfo);
          that.setData({
            userInfo: res.userInfo,
          })
        })
      }
    })
  },
  get_day(){
    let that=this;
    t.request('/users/geDay',{},function(r){
          let days=r.result;
          console.log(days);
          that.setData({
            days:days
          })
    })
  },
  get_info(e){
    var that=this
    t.request('/users/info',{},function(e){
      console.log(e);
      if (e.code==200) {
        that.setData({
          info:e.result
        })
        if(e.result.cars > 0)
        t.globalData.show_login = false;
      }

    })
  },
  to_page(e){
    // if (t.globalData.show_login) {t.show_login();return;}
    var page = e.currentTarget.dataset.index
    if (page == 5) {
      wx.navigateTo({
        url: '/pages/personCenter/set/set',
      })
    }else if (page<4) {
      wx.navigateTo({
        url: '/pages/personCenter/myOrder/myOrder?page=' + page,
      })
    }else if(page == 4){
      wx.makePhoneCall({
        phoneNumber: '15040490888',
      })
    }else if(page == 'store'){
      wx.navigateTo({
        url: '/pages/shopManage/shopManage',
      })
    }
  },
  logon(e){
    this.login.getUserProfile()
  },
  navigate(e){
    // if (t.globalData.show_login) {t.show_login();return;} //不判断登录
    var page
    switch (e.currentTarget.dataset.page) {
      case 'invest':
        page = '/pages/personCenter/chooseStore/chooseStore'
        break;
      case 'mysc':
        page = '/pages/personCenter/mySc/mySc'
        break;
      case 'my_car':
        page = '/pages/personCenter/myCar/myCar'
        break;
      case 'article':
        page = '/pages/article/article?id=' + e.currentTarget.dataset.id
        break;
      case 'detail1':
        page = '/pages/personCenter/shopDetail/shopDetail?index=1'
        break;
      case 'detail2':
        page = '/pages/personCenter/shopDetail/shopDetail?index=0'
        break;
      case 'my_score':
        page = '/pages/personCenter/myScore/myScore?score=' + e.currentTarget.dataset.score
        break;
      default:
        break;
    }
    console.log(page);
    wx.navigateTo({
      url: page,
    })
  },
  callPhone() {
    wx.makePhoneCall({
     phoneNumber: '4000411226' //仅为示例，并非真实的电话号码
    })
  },
  
})